<?php
namespace Crm\Domain;

use Crm\Common\MetaData as MetaData;
use Crm\Common\Util as Util;

class Analytics
{
	/**
	 * pdo连接。
	 * 
	 * @var object
	 * @access pdo
	 */
	public $pdo;

    public $metaData;
    public $baseCode;
    public $city_code;
    public $provinceCode;

    public $contract_purpose_option;//租购目的(用途)
    public $subject_option;

    public function __construct()
    {
        $this->pdo = \PhalApi\DI()->pdo;
        $this->baseCode = (object)\PhalApi\DI()->config->get('basecode');
        $this->province_code = \PhalApi\DI()->config->get('app.provinceCode');//省
        $this->city_code = \PhalApi\DI()->config->get('app.cityCode');//城市

        $this->metaData = new MetaData();

        $this->subject_option = $this->metaData->getPairBaseCodeByType(241,' order by sort desc');
    }

    public function tabbar($state='assess'){
        $tabbale = '<ul class="nav nav-tabs">';
        if($this->session->user_type==5 || $this->session->user_type==6 || $this->session->user_type==4 || $this->session->user_type==3 || $this->session->user_type==1){
            $lilist['contract'] = array('url' => 'contract', 'icon' => 'fa fa-cny mrs', 'title' => '合同业绩');
            $lilist['contract_detail'] = array('url' => 'contractDetail', 'icon' => 'fa fa-cny mrs', 'title' => '合同业绩明细');
        }
        $lilist['perf_rank']=array('url'=>'perfRank','icon'=>'fa fa-bullhorn mrs','title'=>'工作考核');
        $lilist['assess']=array('url'=>'assess','icon'=>'fa fa-bullhorn mrs','title'=>'WI分考核');
        if($this->session->user_type==5 || $this->session->user_type==6 || $this->session->user_type==4 || $this->session->user_type==1) {
            //$lilist['finance'] = array('url' => 'finance', 'icon' => 'fa fa-cny mrs', 'title' => '财务报表');
            //$lilist['salary']=array('url'=>'salary','icon'=>'fa fa-cny mrs','title'=>'工资报表');
            $lilist['customer_visit'] = array('url' => 'customerVisit', 'icon' => 'fa fa-cny mrs', 'title' => '客户带看明细');
            $lilist['customer_origin'] = array('url' => 'customerOrigin', 'icon' => 'fa fa-cny mrs', 'title' => '客户来源分析');
        }

        foreach($lilist as $key=>$val){
            $icon = '';
            if(empty($val['icon']))$icon = '<i class="'.$val['icon'].'"></i>';
            if($key==$state){
                $tabbale.= '<li class="active"><a href="javascript:void(0);">'.$icon.$val['title'].'</a></li>';
            }else{
                $tabbale.= '<li><a href="'.$val['url'].'">'.$icon.$val['title'].'</a></li>';
            }
        }
        $tabbale .= '</ul>';
        return $tabbale;
    }

    /* 统计搜索 */
    public function search(){
        $orderField = isset($_GET['arr_sort']) ? $_GET['arr_sort'] : '';
        $orderValue = isset($_GET['arr_sort_value']) ? $_GET['arr_sort_value'] : '';

        $filter = array();//筛选条件
        $filter['arr_sort'] = $orderField;
        $filter['arr_sort_value'] = $orderValue;
        $page_info ="";

        //账号状态
        $filter['active'] = isset($_GET['active']) ? intval($_GET['active']) : 1;//在职
        $page_info .='active='.$filter['active']."&";

        //部门
        if($this->session->isadmin || $this->session->user_type==1){
            $filter['parent_id'] = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : 50;//默认市场部
        }else{
            $filter['parent_id'] = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : intval($_SESSION['department_id']);//默认市场部
        }
        $page_info .='parent_id='.$filter['parent_id']."&";

        //统计日期
        $date = trim(isset($_GET["date"])?$_GET["date"]:'today');
        $filter['date'] = $date;
        $filter['start_date'] = $_GET['start_date'] ?  strtotime(date('Y-m-d 00:00:00',strtotime($_GET['start_date']))) : strtotime(date('Y-m-d 00:00:00',time()));
        $filter['end_date'] = $_GET['end_date'] ?  strtotime(date('Y-m-d 23:59:59',strtotime($_GET['end_date']))) : strtotime(date('Y-m-d 23:59:59',time()));
        $page_info .='date='.$filter['date']."&start_date=".$_GET['start_date']."&end_date=". $_GET['end_date'];

        $this->loadModel('sys_user');
        $this->loadModel('cyzt_companylist');
        $result = $this->sys_user->getUserList($filter,100);
        $orgSelect = $this->cyzt_companylist->getOrgSelectHtml($filter['parent_id'],$this->session->company_id,'department',140,'onchange="search()"');
        $result['orgSelect'] = $orgSelect;
        //$result['cnt'] = 0;
        $result['filter'] = $filter;
        $result['page_info'] = $page_info;
        //$result['splitPageStr'] = $splitPageStr;
        //$result['next_page'] = $currentPage+1;
        //$result['hasmore'] = (($pageSize*$currentPage)<=$recordCount)?1:0;
        return $result;
    }

    /* 增加kpi */
    public function addKpi($user_id,$data){
        //每月给用户创建一条kpi信息
        $year  = isset($data['year'])?intval($data['year']):'0';
        $month = isset($data['month'])?intval($data['month']):'0';
        if($year>0 && $month>0) {
            //判断该用户本月是否创建过绩效
            $sql = "select count(*) as cnt from crm_kpi_month where kpi_uid={$user_id} and year={$year} and month={$month} and flag=1";
            $cnt = $this->pdo->getRow($sql);
            if($cnt['cnt']==0) {
                //新增数据
                $data['kpi_uid'] = $user_id;
                unset($data['id']);
                $rlt = $this->pdo->add($data, 'crm_kpi_month');
                if ($rlt) {
                    return $rlt;
                }
            }else{
                //更新数据
                unset($data['id']);
                $rlt = $this->pdo->update($data,'crm_kpi_month','kpi_uid='.$user_id.' and year='.$year.' and month='.$month);
                if ($rlt) {
                    return $rlt;
                }
            }
        }
        return false;
    }

    /* 更新kpi */
    public function updateKpiByType($user_id,$type,$year,$month){
        //更新数据
        if($type!='' && $year>0 && $month>0) {
            //判断该用户本月是否创建过绩效
            $sql = "select count(id) as cnt from crm_kpi_month where kpi_uid={$user_id} and year={$year} and month={$month} and flag=1";
            $cnt = $this->pdo->getRow($sql);
            if($cnt['cnt']==1) {
                $where = 'kpi_uid=' . $user_id . ' and year=' . $year . ' and month=' . $month;
                $sql = "update crm_kpi_month set " . $type . "=" . $type . "+1 where " . $where;
                $rlt = $this->pdo->doSql($sql);

                $this->updateKpiScore($user_id,$year,$month);

                if ($rlt) {
                    return $rlt;
                }
            }else{
                $data = array('kpi_uid'=>$user_id,'year'=>$year,'month'=>$month,$type=>1);
                $rlt = $this->pdo->add($data, 'crm_kpi_month');
                if ($rlt) {
                    return $rlt;
                }
            }
        }
        return false;
    }

    /* 更新kpi积分 */
    public function updateKpiScore($user_id,$year,$month){
        $sql = "select * from crm_kpi_month where kpi_uid={$user_id} and year={$year} and month={$month} and flag=1";
        $info = $this->pdo->getRow($sql);
        $kpi_score = $this->countKpiScore($info);
        $info['kpi_score'] = $kpi_score;
        $rlt = $this->pdo->update($info,'crm_kpi_month','kpi_uid='.$user_id.' and year='.$year.' and month='.$month);
        if ($rlt) {
            return $rlt;
        }
        return false;
    }

    /* 计算kpi积分 */
    public function countKpiScore($data){
        $wi = $data['key_num']*$this->config->wi_option['key'];
        $wi = $wi+($data['esf_num']*$this->config->wi_option['esf']);
        //$wi = $wi+($data['customer_num']*$this->config->wi_option['customer']);
        $wi = $wi+($data['price_num']*$this->config->wi_option['price']);
        $wi = $wi+($data['shikan_num']*$this->config->wi_option['shikan']);
        $wi = $wi+($data['miantan_num']*$this->config->wi_option['miantan']);
        $wi = $wi+($data['visit_num']*$this->config->wi_option['visit']);
        $wi = $wi+($data['together_num']*$this->config->wi_option['together']);
        $wi = $wi+($data['contract_num']*$this->config->wi_option['contract']);
        if(isset($data['addition_score']) && $data['addition_score']>0){
            $wi = $wi+$data['addition_score'];
        }
        if(isset($data['subtract_score']) && $data['subtract_score']>0){
            $wi = $wi-$data['subtract_score'];
        }
        return $wi;
    }

    /* 根据用户ID获取统计数量 */
    public function count($table,$data,$filter='create_date'){
        $where = " flag=1";
        //创建者
        $create_uid = isset($data['create_uid'])?$data['create_uid']:0;
        if($create_uid != 0){
            $where .= " and create_uid = ".intval($create_uid);
        }
        //陪看者
        $together_uid = isset($data['together_uid'])?$data['together_uid']:0;
        if($together_uid != 0){
            $where .= " and together_uid like ('%,".intval($together_uid).",%')";
        }
        //拥有者
        $owner_uid = isset($data['owner_uid'])?$data['owner_uid']:0;
        if($owner_uid != 0){
            $where .= " and owner_uid = ".intval($owner_uid);
        }
        //钥匙者
        $key_uid = isset($data['key_uid'])?$data['key_uid']:0;
        if($key_uid != 0){
            $where .= " and key_uid = ".intval($key_uid);
        }
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and ".$filter." >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and ".$filter." <= ".$end_date;
        }

        //echo date('Y-m-d H:i:s',$start_date).'-'. date('Y-m-d H:i:s',$end_date).'----';
        $sql = "SELECT count(*) as cnt FROM {$table} WHERE ".$where;
        //if($table=='crm_contract')echo $sql;
        $cnt = $this->pdo->getRow($sql);
        return $cnt['cnt'];
    }

    /* 统计合同 */
    public function countContract($data){
        $where = " flag=1";
        //签约者
        $contract_uid = isset($data['contract_uid'])?$data['contract_uid']:0;
        if($contract_uid != 0){
            $where .= " and contract_uid = ".intval($contract_uid);
        }
        //合同相关
        $trade_type = isset($data['trade_type'])?$data['trade_type']:0;
        if($trade_type != 0){
            $where .= " and trade_type = ".$trade_type;
        }
        //签约开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and contract_date >= ".$start_date;
        }
        //签约结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and contract_date <= ".$end_date;
        }

        $sql = "SELECT count(*) as cnt FROM crm_contract WHERE ".$where;
        //echo $sql;exit;
        $cnt = $this->pdo->getRow($sql);
        return $cnt['cnt'];
    }

    /* 分成统计[按签约时间] */
    public function countPerf($data){
        $where = " a.flag=1 and a.action_type='crm_contract' ";
        //绩效人
        $perf_uid = isset($data['contract_uid'])?$data['contract_uid']:0;
        if($perf_uid != 0){
            $where .= " and a.perf_uid = ".intval($perf_uid);
        }
        //合同相关
        $trade_type = isset($data['trade_type'])?$data['trade_type']:0;
        if($trade_type != 0){
            $where .= " and b.trade_type = ".$trade_type;
        }
        //签约开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and b.contract_date >= ".$start_date;
        }
        //签约结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and b.contract_date <= ".$end_date;
        }
        //统计该用户的绩效比例
        $sql = "select a.ratio,a.action_id,b.status from crm_perf as a LEFT JOIN crm_contract as b on a.action_id=b.id where ".$where;
        $rlt = $this->pdo->getAll($sql);
        $cnt['wi'] = 0;
        $cnt['obtain_money'] = 0;  //应得分成业绩 = 总利润*百分比
        $cnt['obtain_moneyed'] = 0;//实得分成业绩 = 实际利润*百分比
        $this->loadModel('crm_finance');
        foreach ($rlt as $key=>$item){
            $cnt['wi'] += $item['ratio'];
            $perfMoney = $this->crm_finance->getInvoicesMoneyByContractId($item['action_id']);
            //应得分成业绩金额=总利润*百分比
            $cnt['obtain_money'] += round(($perfMoney['profit'] * $item['ratio']) / 100, 2);
            //实得分成业绩金额=实际利润*百分比
            $cnt['obtain_moneyed'] += round(($perfMoney['profited'] * $item['ratio']) / 100, 2);
        }
        return $cnt;
    }

    /* 实收分成统计[按合同最后结佣[收款]时间] finish_invoices_date */
    public function countPerf2($data){
        $where = " a.flag=1 and a.action_type='crm_contract' ";
        //绩效人
        $perf_uid = isset($data['contract_uid'])?$data['contract_uid']:0;
        if($perf_uid != 0){
            $where .= " and a.perf_uid = ".intval($perf_uid);
        }
        //合同相关
        $trade_type = isset($data['trade_type'])?$data['trade_type']:0;
        if($trade_type != 0){
            $where .= " and b.trade_type = ".$trade_type;
        }
        //签约开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and b.finish_invoices_date >= ".$start_date;
        }
        //签约结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and b.finish_invoices_date <= ".$end_date;
        }
        //统计该用户的绩效比例
        $sql = "select a.ratio,a.action_id,b.status from crm_perf as a LEFT JOIN crm_contract as b on a.action_id=b.id where ".$where;
        $rlt = $this->pdo->getAll($sql);
        $cnt['finish_obtain_moneyed'] = 0;  //只统计已经结佣或履行完毕的合同分成=实际利润*百分比
        $cnt['contract_ids'] = '';  //关联合同
        $this->loadModel('crm_finance');
        foreach ($rlt as $key=>$item){
            $perfMoney = $this->crm_finance->getInvoicesMoneyByContractId($item['action_id']);
            $cnt['contract_ids'] .= $item['action_id'].',';
            //只统计已经结佣或履行完毕的合同分成=实际利润*百分比
            $cnt['finish_obtain_moneyed'] += round(($perfMoney['profited'] * $item['ratio']) / 100, 2);
        }
        return $cnt;
    }

    /* 根据业务模型、用户ID获取统计跟进数量 */
    public function countLog($data){
        $where = " a.flag=1";
        //创建者
        $create_uid = isset($data['create_uid'])?$data['create_uid']:0;
        if($create_uid != 0){
            $where .= " and a.create_uid = ".intval($create_uid);
        }
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and a.create_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and a.create_date <= ".$end_date;
        }
        //跟进类型
        $subject = isset($data['subject'])?$data['subject']:'';
        if($subject != ''){
            $where .= " and a.subject = '".$subject."'";
        }
        if($subject=='1010801' || $subject=='1010802'){//加减分
            $sql = "SELECT a.id,a.subject,a.des FROM crm_log as a WHERE " . $where;
            $list = $this->pdo->getAll($sql);
            $rlt['cnt'] = count($list);
            $score = 0;
            foreach ($list as $item){
                $score += $item['des'];
            }
            $rlt['score'] = $score;
            return $rlt;
        }else {
            //echo date('Y-m-d H:i:s',$start_date).'-'. date('Y-m-d H:i:s',$end_date).'----';
            //$sql = "SELECT count(*) as cnt FROM crm_log as a left join {$table} as b on a.id=b.log_id  WHERE ".$where;
            $sql = "SELECT count(id) as cnt FROM crm_log as a WHERE " . $where;
            //if($create_uid==4)echo $sql;
            $cnt = $this->pdo->getRow($sql);
            return $cnt['cnt'];
        }
    }

    /* 根据用户ID获取统计数量 */
    public function countVisit($data){
        $where = " flag=1";
        //创建者
        $create_uid = isset($data['create_uid'])?$data['create_uid']:0;
        if($create_uid != 0){
            $where .= " and create_uid = ".intval($create_uid);
        }
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and create_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and create_date <= ".$end_date;
        }

        $sql = "SELECT id,FROM_UNIXTIME(`create_date`,'%Y%m%d') as ctime,customer_id,visit_type FROM crm_visit WHERE ".$where." order by visit_type desc";
        $list = $this->pdo->getAll($sql);
        $_list = array();
        foreach ($list as $key=>$item){
            if(isset($_list[$item['ctime'].$item['customer_id']])){
                continue;
            }else{
                $_list[$item['ctime'].$item['customer_id']] = $list[$key];
            }
        }
        $cnt['miantan_num'] = 0;
        $cnt['visit_num'] = 0;
        foreach ($_list as $key=>$item){
            if($item['visit_type'] == '1010601'){
                $cnt['miantan_num']=$cnt['miantan_num']+1;
            }else{
                $cnt['visit_num']=$cnt['visit_num']+1;
            }
        }
        return $cnt;
    }

    /* 根据用户ID获取统计数量 */
    public function countTogether($data){
        $where = " flag=1";
        //陪看者
        $together_uid = isset($data['together_uid'])?$data['together_uid']:0;
        if($together_uid != 0){
            $where .= " and together_uid like ('%,".intval($together_uid).",%')";
        }
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and create_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and create_date <= ".$end_date;
        }
        $sql = "SELECT id,FROM_UNIXTIME(`create_date`,'%Y%m%d') as ctime,customer_id FROM crm_visit WHERE ".$where." order by id desc";
        $list = $this->pdo->getAll($sql);
        $_list = array();
        foreach ($list as $key=>$item){
            if(isset($_list[$item['ctime'].$item['customer_id']])){
                continue;
            }else{
                $_list[$item['ctime'].$item['customer_id']] = $list[$key];
            }
        }
        return count($_list);
    }

    /* 根据业务模型、用户ID获取统计跟进数量 */
    public function countRecord($table,$data){
        $where = " ";
        //创建者
        $owner_uid = isset($data['owner_uid'])?$data['owner_uid']:0;
        if($owner_uid != 0){
            $where .= " a.owner_uid = ".intval($owner_uid);
        }
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and a.create_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and a.create_date <= ".$end_date;
        }
        $sql = "SELECT count(*) as cnt FROM ".$table." as a WHERE ".$where;
        $cnt = $this->pdo->getRow($sql);
        return $cnt['cnt'];
    }

    /* 统计时间段内 签约合同金额 */
    public function countContractMoneyByUserId($user_id,$data){
        $where = " a.flag=1 and a.contract_uid=".$user_id." ";
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and a.contract_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and a.contract_date <= ".$end_date;
        }
        $sql = "SELECT id FROM crm_contract as a WHERE ".$where;
        $contractList = $this->pdo->getAll($sql);
        $this->loadModel('crm_finance');
        $rlt['obtain_money'] = 0;
        $rlt['obtain_moneyed']= 0;
        $rlt['pay_money'] = 0;
        $rlt['pay_moneyed']= 0;

        $rlt['perf_money'] = 0;//绩效提成总额
        foreach ($contractList as $key=>$item) {
            $money = $this->crm_finance->getInvoicesMoneyByContractId($item['id']);
            $rlt['obtain_money'] += $money['obtain_money'];
            $rlt['obtain_moneyed'] += $money['obtain_moneyed'];
            $rlt['pay_money'] += $money['pay_money'];
            $rlt['pay_moneyed'] += $money['pay_moneyed'];
        }
        return $rlt;
    }

    /* 统计时间段内所有合同的应收、付总金额 */
    public function countInvoicesMoneyByUserId($user_id,$data){
        $where = " a.flag=1 and a.contract_uid=".$user_id." ";
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and a.contract_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and a.contract_date <= ".$end_date;
        }
        //找到该用户的所有合同
        $sql = "SELECT id FROM crm_contract as a WHERE ".$where;
        $contractList = $this->pdo->getAll($sql);
        $this->loadModel('crm_finance');
//      $this->loadModel('crm_perf');
        $rlt['obtain_money'] = 0;
        $rlt['obtain_moneyed']= 0;
        $rlt['pay_money'] = 0;
        $rlt['pay_moneyed']= 0;

        $rlt['perf_money'] = 0;//绩效提成总额
        foreach ($contractList as $key=>$item) {
            //$money = $this->crm_finance->getInvoicesMoneyByContractId($item['id'],$start_date,$end_date);
            $money = $this->crm_finance->getInvoicesMoneyByContractId($item['id']);
            $rlt['obtain_money'] += $money['obtain_money'];
            $rlt['obtain_moneyed'] += $money['obtain_moneyed'];
            $rlt['pay_money'] += $money['pay_money'];
            $rlt['pay_moneyed'] += $money['pay_moneyed'];
            $rlt['obtain_profit_money'] += ($money['obtain_money']-$money['pay_money']);//应收利润
//            //计算用户多个合同的提成总额
//            $perfMoney = $this->crm_perf->getPerfMoneyByContractId($item['id'], $user_id);
//            if (isset($perfMoney['id'])) {
//                $rlt['perf_obtain_money'] += ($perfMoney['ratio']*$money['obtain_money'])/100;
//                $rlt['perf_receive_money'] += ($perfMoney['ratio']*$money['receive_money'])/100;
//            }
        }
        return $rlt;
    }

    /* 统计该用户的应收、付金额 */
    public function countInvoicesMoneyByUserId2($user_id,$data){
        $where = " a.flag=1 and b.contract_uid=".$user_id." ";
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and a.contract_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and a.contract_date <= ".$end_date;
        }
        //类型(1应收款、2应付款)
        $type = isset($data['type'])?$data['type']:0;
        if($type != 0){
            $where .= " and a.type <= ".$type;
        }
        $sql = "SELECT a.id,a.money,a.type FROM crm_invoices as a LEFT JOIN crm_contract as b ON a.contract_id=b.id WHERE ".$where;
        $invoicesList = $this->pdo->getAll($sql);

        $this->loadModel('crm_finance');
        $rlt['obtain_money'] = 0;
        $rlt['obtain_moneyed'] = 0;
        $rlt['pay_money']= 0;
        $rlt['pay_moneyed']= 0;
        foreach ($invoicesList as $key=>$item){
            $money = $this->crm_finance->getInvoicesOrderMoney($item['id']);
            if($item['type']==1) {
                $rlt['obtain_money'] += $item['money'];
                $rlt['obtain_moneyed'] += $money;
            }else{
                $rlt['pay_money'] += $item['money'];
                $rlt['pay_moneyed'] += $money;
            }
        }
        return $rlt;
    }

}

